layui.use(['layer', 'form', 'admin', 'ax', 'table', 'jquery'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var table = layui.table;

    //初始化详情数据
    var ajax = new $ax(Feng.ctxPath + "/product/view/" + Feng.getUrlParam("productId"));
    var result = ajax.start();
    form.val('productEditForm', result.data);


// 初始化所有的产品类型列表
    $.ajax({
        url: Feng.ctxPath + '/productType/list',
        dataType: 'json',
        type: 'get',
        data: {},
        success: function (data) {
            $.each(data.data, function (index, value) {
                $('#productTypeId').append(new Option(value.name, value.id));// 下拉菜单里添加元素
            });
            layui.form.render('select');
        }
    });
    var productTypeId = result.data.productTypeId;
    var ajax = new $ax(Feng.ctxPath + "/productAttr/detail/" + productTypeId);
    var productAttr = ajax.start();
    var attrContext = productAttr.data.attrContext;
    var oldTableData = [];
    if (attrContext != null) {
        var oldAttrJson = JSON.parse(attrContext);
        for (var key in oldAttrJson) {
            var obj = {'id': result.data.attrId};
            obj['productAttrName'] = key;
            obj['productAttr'] = '';
            obj['productAttrValue'] = oldAttrJson[key];
            oldTableData.push(obj);
        }
        //渲染属性值
        var tableData = [];
        var exAttr = result.data.exAttr;
        if (exAttr != null) {
            var attrJson = JSON.parse(exAttr);
            var i = 0
            oldTableData.forEach(function (data) {
                i++
                var obj2 = {}
                var find = false;
                for (var key in attrJson) {
                    if (data.productAttrName == key && data.productAttrValue == attrJson[key].split("_")[1]) {
                        obj2.id = result.data.attrId + "_" + i
                        obj2.productAttrName = data.productAttrName;
                        obj2.productAttr = attrJson[key].split("_")[0];
                        obj2.productAttrValue = data.productAttrValue;
                        find=true;
                        break;
                    }
                }
                if(!find){
                    obj2.id = result.data.attrId + "_" + i
                    obj2.productAttrName = data.productAttrName;
                    obj2.productAttr = "";
                    obj2.productAttrValue = data.productAttrValue;
                }
                tableData.push(obj2);
                find=false;

            })
        }
        table.render({
            elem: '#productAttrTable',
            id: "productAttrTable",
            cellMinWidth: 95,
            // toolbar: "#toolbar",
            defaultToolbar: [],
            height: "full-160",
            text: {none: "暂无数据"},
            page: false,
            skin: 'line ',
            cols: [
                [
                    {
                        field: 'id',
                        title: 'ID',
                        hide: true
                    },
                    {
                        type: "checkbox",
                        fixed: 'left',
                        width: "10%"
                    },
                    {
                        field: 'productAttrName',
                        title: '属性名称',
                        width: "30%",
                        align: 'center'
                    },
                    {
                        field: 'productAttr',
                        title: '值',
                        width: "30%",
                        align: 'center',
                        edit: 'text'
                    },
                    {
                        field: 'productAttrValue',
                        title: '单位',
                        width: "30%",
                        align: 'center'
                    }
                ]
            ],
            data: tableData
        });

    }


    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var attrData = table.cache["productAttrTable"];
        var json = {};

        for (var i = 0; i < attrData.length; i++) {
            var attr = attrData[i];
            if (attr.productAttr) {
                if (!data.field.attrId) {
                    data.field.attrId = attr.id.split("_")[0]
                }
                json[attr.productAttrName] = attr.productAttr + "_" + attr.productAttrValue;
                data.field.exAttr = JSON.stringify(json);
            }

        }


        var ajax = new $ax(Feng.ctxPath + "/product/edit", function (data) {
            Feng.success("修改成功!");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message + "!");
        });
        ajax.set(data.field);
        ajax.start();

        //添加 return false 可成功跳转页面
        return false;
    });
});